/**
 * Created by kevin on 16/8/16.
 */
var eosi = window.eosi || {};
this.eosi.fotile = this.eosi.fotile || {};
//this.eosi.fotile = this.eosi.fotile || {};
//this.eosi.fotile = function(name) {
//    this.name = name;
//};

(function (window) {
    // the application

    function Fotileloader(fotile,images,call,showUI) {
        this.version = fotile.version
        this.Container_constructor();//调用父类的构造函数
        this.init(fotile,images,call,showUI);
    }
    var p = createjs.extend(Fotileloader, createjs.Container);
    // public methods:
    p.init = function (fotile,images,call,showUI) {
        this.fotile = fotile;
        console.log("I'm robot loader!!!");
        if (showUI) {
            this.UIloader(images,call,1);
        }else{
            this.setupLoaderUI(images,call);
        }
    }
    p.UIloader = function (images,call,stop){
        master_UILoader=null;
        // console.log(call);
        fotile=this;
        var manifest =images;
        var master = this;
        master_UILoader = new createjs.LoadQueue(true);
        master_UILoader.on("fileload", masterFileLoaded);
        master_UILoader.on("error", masterUILoadError);
        master_UILoader.on("complete", masterUILoadComplete);
        master_UILoader.on("progress", loader_update,true);
        master_UILoader.loadManifest(manifest, true, "./_assets/");
        //加载完毕
        function masterUILoadComplete(event){
            // fotile.setupLoaderUI();
            call();
        }
        //文件加载失败
        function masterUILoadError(event) {
            console.log("玩儿命加载中..."+event.target);
        }
        function masterFileLoaded(event){
        }
            //加载状态更新
        function loader_update(master_UILoader) {
            if (!stop) {
               fotile.progress.text=(master_UILoader.progress * 100 | 0) + "%";
            }
        }
    }
    p.setupLoaderUI = function (images,call){
        var bn = new createjs.Bitmap(this.fotile.resLoader.getResult("loading"));
        var bg = new createjs.Bitmap(this.fotile.resLoader.getResult("loading_bg"));
        var text = new createjs.Text("玩儿命加载中...", "3vh Arial", "#000000");
        this.progress = new createjs.Text("0%", "2.4vh Arial", "#000000");
        //bg png
        var v_w = bg.getBounds().width;
        var v_h = bg.getBounds().height;
        var w = this.fotile.getWidth();
        var h = this.fotile.getHeight();
        var ratio_y = h/v_h;
        var ratio_x =  w/v_w;
        bg.setTransform(0,0,ratio_x,ratio_y);
        bg.x=0;
        bg.y=0;
        //bn png
        bn.setTransform(0,0,ratio_y,ratio_y);
        bn.alpha = 0;
        bn.regX = bn.getBounds().width >> 1;
        bn.regY = bn.getBounds().height >> 1;
        bn.x = this.fotile.getWidth()/2;
        bn.y = (this.fotile.getHeight()/2)-(bn.getBounds().height*ratio_x/2);
        createjs.Tween.get(bn)
            .to({ y: (this.fotile.getHeight()/2)-(bn.getBounds().height*ratio_x/2),alpha:1}, 500, createjs.Ease.sineOut);

        // Center the text
        var bounds = text.getBounds();
        text.lineWidth = this.fotile.getWidth();
        text.lineHeight = 22;
        text.textBaseline = "top";
        text.textAlign = "center";
        text.x =this.fotile.getWidth()/2;
        text.y =(this.fotile.getHeight()/2)+100;

        this.progress.lineWidth = this.fotile.getWidth();
        this.progress.lineHeight = 22;
        this.progress.textBaseline = "top";
        this.progress.textAlign = "center";
        this.progress.x =this.fotile.getWidth()/2;
        this.progress.y =(this.fotile.getHeight()/2-(bn.getBounds().height*ratio_x/2)-10);
        this.progress.alpha = 0;
        createjs.Tween.get(this.progress)
            .wait(500)
            .to({alpha:1}, 700, createjs.Ease.sineOut);
        this.addChild(bg);
        this.addChild(bn);
        this.addChild(text);
        this.addChild(this.progress);

        createjs.Ticker.addEventListener("tick", handleTick);
        function handleTick(event) {
         // 每个tick实施运行(又名帧)
            if (!event.paused) {
                //动画
                bn.rotation += event.delta * 0.5;
                stage.update(event);
            }
        }
        (function settext(){
            //设置为全局 以后要用其它字直接换
            this.text1=[
                '玩儿命加载中...',
                '等等我，我在酝酿洪荒之力',
                '不要怀疑网速，我真的有在加载',
                '进度条君总是会挂掉的',
                '给你时间叫人啊！',
                '双人来玩儿有大礼啊！',
                '没叫来人？绑票啊！',
                '4999元基金，就是要给耐得住性子的人！',
                '拼命加载，鬼知道我经历了什么',
                '别走开啊，你真的不要我了么',
                '恭喜你，顽皮的进度条君真的要挂了',
            ];

            var time=0;
            var setfont=this.setInterval(function(){
                    text.text=this.text1[time++];
                    if (time>this.text1.length) {
                        time=0;
                    }
            },1000);
        })(this.fotile);
        this.clearLoading=function (){
            createjs.Ticker.removeEventListener("tick", handleTick);
            this.removeChild(bn);
            this.removeChild(bg);
            this.removeChild(text);
            this.removeChild(this.progress);
            stage.update();
        }
        this.UIloader(images,call);
    }

    window.eosi.fotile.Fotileloader = createjs.promote(Fotileloader, "Container");
}(window));
